package xyz.naoyu.servlets.service.impl;

import com.alibaba.fastjson.JSON;
import xyz.naoyu.dao.BaseDao;
import xyz.naoyu.entity.Talk;
import xyz.naoyu.servlets.service.TalkService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yWieLoc
 * @date 2022/3/18 23:14
 */

@WebServlet("/api/talk")
public class TalkServlet extends BaseServlet implements TalkService {

    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Talk talk = new Talk(req.getParameter("Id"));


        try {
            // 后端信息
            talk = (Talk) talk.getInfo(tools);


            // 新建
            if(talk.getCreatorId() == null) {
                talk = JSON.parseObject(req.getParameter("talk"),talk.getClass());

                // 设定系统信息
                talk.setCreatorId((String) req.getSession().getAttribute("Id"));
                talk.setCreateTime(String.valueOf(System.currentTimeMillis()));

                // 更新
            } else  {
                // 用户编辑，认证
                if(talk.getCreatorId() != req.getSession().getAttribute("Id") ) {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(false,"无权限",null)
                    ));
                    return;
                }
                // 加载参数
                talk = JSON.parseObject(req.getParameter("talk"),talk.getClass());
                talk.setType("Talk");
            }


            // 传向后端
            if(talk.infoUpdate(tools) == 0) {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false,"更新失败",null)
                ));
                return;
            } else {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(true,"更新成功",null)
                ));
                return;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void del(HttpServletRequest req, HttpServletResponse resp) {
        BaseDao tools = new BaseDao();
        Talk talk = new Talk(req.getParameter("Id"));


        try {
            talk = (Talk) talk.getInfo(tools);
            if(talk.getCreatorId()!= null && talk.getCreatorId() == req.getSession().getAttribute("Id")) {
                if(talk.delSelf(tools)) {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(true, null,null)
                    ));
                }else {
                    resp.getWriter().append(JSON.toJSONString(
                            new BackCode(false, "删除失败",null)
                    ));
                }
            } else {
                resp.getWriter().append(JSON.toJSONString(
                        new BackCode(false, "权限不足",null)
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
